package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @create 2020-05-13 19:02
 */
@Api(tags = "后台管理中心")
@RestController
//@CrossOrigin 由网关添加配置类解决跨域问题
@RequestMapping("/admin/product")
public class ManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping("/getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> category1List = manageService.getCategory1();
        return Result.ok(category1List);
    }

    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id) {
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }

    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id) {
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }
    //根据一，二，三级节点属性 获得页面加载集合
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> attrInfoList = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfoList);
    }
    //添加平台属性 saveAttrInfo
    @PostMapping("/saveAttrInfo")  //@RequestBody接收的是一个对象
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //查询SPU分页列表
    @GetMapping("/{page}/{limit}")
    public Result spuPage(@PathVariable(name = "page") Integer page,
                          @PathVariable(name = "limit") Integer limit, Long category3Id) {
        IPage<SpuInfo> spuInfoPage = manageService.spuPage(page, limit, category3Id);
        return Result.ok(spuInfoPage);
    }

    //SPU页面加载的品牌
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> trademarkLists = manageService.getTrademarkList();
        return Result.ok(trademarkLists);
    }

    //查询字典表中 销售属性集合
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr>  baseSaleAttrList =manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    //保存SPU
    @PostMapping("/saveSpuInfo")  //@RequestBody接收的是一个对象
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //保存SKU之前查询图片集合
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){
        List<SpuImage>  spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }
    //根据SpuId查询销售属性及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //保存SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSpuInfo(skuInfo);
        return Result.ok();
    }
    //查询库存分页
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuList(@PathVariable(name = "page")Integer page,@PathVariable(name = "limit")Integer limit){
        IPage<SkuInfo> skuList = manageService.getSkuList(page, limit);
        return Result.ok(skuList);
    }
    //上架
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId")Long skuId){
            manageService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId")Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }


    //分页查询品牌列表  baseTrademark/1/10
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result  baseTrademark(@PathVariable(name = "page")Integer page,@PathVariable(name = "limit")Integer limit){
       IPage<BaseTrademark> baseTrademarkIPage = manageService.baseTrademark(page,limit);
        return Result.ok(baseTrademarkIPage);
    }
    //平台属性id查询属性值
    @GetMapping("/getAttrValueList/{categoryId}")
    public Result getAttrValueList(@PathVariable(name = "categoryId") Long categoryId){
        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(categoryId);
        return Result.ok(baseAttrValueList);
    }



}
